package basic.socket.tcp.thread;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {

    /**
     * Socket客户端
     */
    public static void main(String[] args) {
       // for(int i=0;i<10;i++){
        try {
            //创建Socket对象

            int i=0;
                while(true) {
                    //basic.socket.
                    Socket socket=new Socket("localhost",8888);
                    //basic.socket.
                    i++;
                    //根据输入输出流和服务端连接
                    System.out.println("第" + i + "次");
                    BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(System.in));

                    String msg = bufferedReader1.readLine();

                    OutputStream outputStream = socket.getOutputStream();//获取一个输出流，向服务端发送信息
                    PrintWriter printWriter = new PrintWriter(outputStream);//将输出流包装成打印流
                    System.out.println("你输入的是-->"+msg);
                    printWriter.print(msg);
                    printWriter.flush();
                    socket.shutdownOutput();//关闭输出流

                    InputStream inputStream = socket.getInputStream();//获取一个输入流，接收服务端的信息
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//包装成字符流，提高效率
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);//缓冲区
                    String info = "";
                    String temp = null;//临时变量
                    while ((temp = bufferedReader.readLine()) != null) {
                        info += temp;
                        System.out.println("客户端接收服务端发送信息：" + info);
                    }

                    //关闭相对应的资源
                    bufferedReader.close();
                    inputStream.close();
                    printWriter.close();
                    outputStream.close();
                    socket.close();
                }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //}
    }

}